﻿using BroadGoalComponents.BGControls.Toast;
using BroadGoalComponents.Dialogs;
using Custom.Framework;
using Custom.Model.Entity;
using Custom.Service.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Business.Custom
{
    public partial class frmCustomEdit : frmCustomAdd
    {
        private CustomInfo custom = null;

        public frmCustomEdit(int cusid)
        {
            InitializeComponent();

            this.custom = new CustomInfoRepository().Get(cusid);
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        protected override void initData()
        {
            base.txtCardNo.Text = this.custom.cardno;
            base.ddlLevel.SelectedValue = this.custom.level;
            base.txtName.Text = this.custom.cusname;
            if (this.custom.sex == "0")
            {
                base.rbMan.Checked = true;
            }
            else
            {
                base.rbWoman.Checked = true;
            }
            base.txtIdentityNo.Text = this.custom.identityno;
            base.txtBirthday.Text = this.custom.birthday;
            base.txtAddress.Text = this.custom.address;
            base.txtMobile.Text = this.custom.mobile;
            base.txtQQ.Text = this.custom.qq;
            base.txtEmail.Text = this.custom.email;
            base.txtMemo.Text = this.custom.memo;
        }

        protected override void saveData()
        {
            this.custom.cardno = base.txtCardNo.Text.Trim();
            this.custom.level = base.ddlLevel.SelectedValue;
            this.custom.cusname = base.txtName.Text.Trim();
            this.custom.sex = base.rbMan.Checked ? "0" : "1";
            this.custom.identityno = base.txtIdentityNo.Text.Trim();
            this.custom.birthday = base.txtBirthday.Text.Trim();
            this.custom.address = base.txtAddress.Text.Trim();
            this.custom.mobile = base.txtMobile.Text.Trim();
            this.custom.qq = base.txtQQ.Text.Trim();
            this.custom.email = base.txtEmail.Text.Trim();
            this.custom.memo = base.txtMemo.Text.Trim();

            try
            {
                new CustomInfoRepository().Update(this.custom);

                BGToast.Show("保存成功");

                this.Close();
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                WarningDialog.Show("保存出错啦");
            }
        }

        /// <summary>
        /// 检查会员数据
        /// </summary>
        /// <returns></returns>
        protected virtual bool checkData()
        {
            if (string.IsNullOrEmpty(this.txtCardNo.Text.Trim()))
            {
                WarningDialog.Show(this, "请输入会员卡号");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                WarningDialog.Show(this, "请输入会员姓名");
                return false;
            }

            List<CustomInfo> customs = new CustomInfoRepository().Get(w => w.cardno == this.txtCardNo.Text.Trim() && w.id == this.custom.id);
            if (customs != null && customs.Count() > 0)
            {
                WarningDialog.Show(this, "已存在相同的会员卡号，请重新输入");
                return false;
            }

            return true;
        }
    }
}
